<?php
include("inc/header.php" );
include("module/fckeditor/fckeditor.php") ;
require_once('../inc/mailclass.php');
//require_once('../inc/smtp.php');
login_check( );

?>
<html>
<head>
<title>发送邮件</title>
<style type="text/css">
.black_overlay{display: none;position: absolute;top: 0%;left: 0%;width: 100%;height: 100%;background-color:#FFFFFF;z-index:1001;-moz-opacity: 0.8;opacity:.80;filter: alpha(opacity=80);}
.white_content{display: none;position: absolute;top: 25%;left: 25%;width: 50%;height: 50%;padding: 10px;border: 10px solid #bbb;margin:-20px;background-color: white;z-index:1002;overflow: auto;}
</style>
<script language="javascript" type="text/javascript">
function moveselect(obj,target,all){
	if (!all) all=0
	if (obj!="[object]") obj=eval("document.all."+obj)
	target=eval("document.all."+target)
	if (all==0)
	{
		while (obj.selectedIndex>-1){
			//alert(obj.selectedIndex)
			mot=obj.options[obj.selectedIndex].text
			mov=obj.options[obj.selectedIndex].value
			obj.remove(obj.selectedIndex)
			var newoption=document.createElement("OPTION");
			newoption.text=mot
			newoption.value=mov
			target.add(newoption)
		}
	}
	else
	{
		//alert(obj.options.length)
		for (i=0;i<obj.length;i++)
		{
			mot=obj.options[i].text
			mov=obj.options[i].value
			var newoption=document.createElement("OPTION");
			newoption.text=mot
			newoption.value=mov
			target.add(newoption)
		}
		obj.options.length=0
	}
}
function dakai(){
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block'
}
function guanbi(){
	//下面上把右边select的值传到文本框内
	var accepter=document.getElementById("accepter")
	accepter.value=""//如果不加这句，则每次选择的结果追加
	var huoQu=document.getElementById("D2")
	for(var k=0;k<huoQu.length;k++)
	accepter.value=accepter.value + huoQu.options[k].value + ";"//这里为字符间的分隔符
	document.getElementById('light').style.display='none';
	document.getElementById('fade').style.display='none'
}
</script>
</head>
<body>
<?php
$result = mysql_query('SELECT * FROM tun2_email_set');
$row = mysql_fetch_assoc($result);

//print_r($row);
if ($_GET['Action'] == 'send') {
	$toArray = array_flip(array_flip(array_filter(explode(';',$_POST['accepter'])))); //array_flip用于将数组中的键与值交换，两次重复使用可删除数组中重复的元素，并保留最后一个元素;array_filter(利用回调函数来对数组进行过滤)过滤掉其中的空值
	if (!isset($_POST['mailtitle']) || empty($_POST['mailtitle']) || strlen($_POST['mailtitle'])==0) {
		admin_showerr( "<li>邮件标题不能为空</li>", "Admin_Email_Send.php", 0);
	}
	$subject = "=?UTF-8?B?".base64_encode($_POST['mailtitle'])."?=";
	$body    = $_POST['content'];
	if (is_array($toArray) && count($toArray)>0) {
		$pre = '/^([a-z0-9]*[-_\.]?[a-z0-9]+)+@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i'; //邮箱正则表达式
		foreach ($toArray as $value) {
			if (!preg_match($pre,$value)) {
				admin_showerr( "<li>收件人邮箱格式填写有误</li>", "Admin_Email_Send.php", 0);
				break;
			}
		}
	}
	if (!isset($body) || strlen($body) == 0) {
		admin_showerr( "<li>邮件正文不能为空</li>", "Admin_Email_Send.php", 0);
	}
	
	$addresser = $_POST['addresser'] ? $_POST['addresser'] : $row['addresser'];
	$body .= "<br>本邮件来自:<b>$addresser</b>";
	$MailConfigArray = array(
					'host'      =>  $row['host'],
					'port'      =>  $row['port'],
					'trueemail' =>  $row['trueemail'],
					'username'  =>  $row['username'],
					'password'  =>  $row['password'],
					'debug'     =>  $row['debug']=='truee' ? true : false,
					//'from'      =>  "=?UTF-8?B?".base64_encode($addresser)."?="
					'from'      =>  $row['trueemail']
				);
	set_time_limit(180); //设置超时180秒
	
	$mail = new Smtp($MailConfigArray);
	$mail->send($toArray,$subject,$body);
	
	addlog( "发送邮件", $_SESSION['user_name'] );
	admin_showerr( sprintf("<li>共有<span style=\"color:red;\">%d</span>封邮件发送成功</li>",count($toArray)), "Admin_Email_Send.php", 1 );
}
?>
  <table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
  <form id="form1" name="form1" method="post" action="Admin_Email_Send.php?Action=send">
    <tr>
      <td align="right">发件人：</td>
      <td><label>
        <input name="addresser" type="text" id="addresser" value="<?=$row['addresser']?>" maxlength="100" />
      </label></td>
    </tr>
    <tr>
      <td align="right">邮件标题：</td>
      <td><label>
        <input name="mailtitle" type="text" id="mailtitle" size="50" maxlength="200" />
        <span style="color:red;">*</span></label></td>
    </tr>
    <tr>
      <td align="right">收件人：</td>
      <td><label>多方发送时用<span style="color:red;"> ; </span>分开邮箱地址，请确保每个地址的准确性，不能出现空格、回车、换行等非法操作符<br />
        <input name="accepter" type="text" size="120" id="accepter"> <a href="javascript:void(0);" onclick="dakai();"><b>选择</b></a>
        <span style="color:red;">*</span></label></td>
    </tr>
    <tr>
      <td align="right">邮件正文：</td>
      <td><?php $oFCKeditor = new FCKeditor('content')  ;
                                $oFCKeditor->BasePath = 'module/fckeditor/' ;   
                                $oFCKeditor->ToolbarSet = 'Default' ;
                                $oFCKeditor->InstanceName = 'content' ;
                                $oFCKeditor->Width = '90%' ;
                                $oFCKeditor->Height = '350' ;
                                $oFCKeditor->Value  = '';
                                $oFCKeditor->Create() ;?>
      <span style="color:red;">*</span></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td>
      <input type="submit" name="Submit" value="发送邮件" /></td>
    </tr>
	</form>
</table>

<div id="light" class="white_content">
<table border="1" width="350" id="table4" bordercolor="#CCCCCC" bordercolordark="#CCCCCC" bordercolorlight="#FFFFFF" cellpadding="3" cellspacing="0">
  <tr>
    <td width="150" height="200" align="center" valign="middle">
     选择收件人邮箱地址
      <select size="12" name="D1" ondblclick="moveselect(this,'D2')" multiple="multiple" style="width:140px">
        <?php       
        $db->query('SELECT * FROM tun2_email WHERE verify=1');
        while (0<$db->next_record()) {
        	echo "<option value=\"".$db->Record['email']."\">".$db->Record['email']."</option>";
        }
        ?>
      </select>
    </td>
    <td width="50" height="200" align="center" valign="middle">
      <input type="button" value="<<" name="B3" onclick="moveselect('D2','D1',1)" /><br />
      <input type="button" value="<" name="B5" onclick="moveselect('D2','D1')" /><br />
      <input type="button" value=">" name="B6" onclick="moveselect('D1','D2')" /><br />
      <input type="button" value=">>" name="B4" onclick="moveselect('D1','D2',1)" /><br />
    </td>
    <td width="150" height="200" align="center" valign="middle">
      当前已选中的邮箱地址
      <select size="12" name="D2" id="D2" ondblclick="moveselect(this,'D1')" multiple="multiple" style="width:140px">
        
      </select>
    </td>
  </tr>
</table>
&nbsp;<input type="button" name="button" onclick="guanbi()" value="确 定" /> <span>选择时按住CTRL键可同时选择多项</span>
</div>
<div id="fade" class="black_overlay"></div>
</body>
</html>
